Skip to main content

Processing & Statistics

Buffer analysis

@fused.udf
def udf(n_points: int=1000, buffer: float=0.0025):
import geopandas as gpd
from shapely.geometry import Point, Polygon, LineString
import random

# Create LineString to represent a road
linestring_columbus = LineString([[-122.4194,37.8065],[-122.4032,37.7954]])
gdf_road = gpd.GeoDataFrame({'geometry': [linestring_columbus], 'name': ['Columbus Ave']})

# Create Points to represent GPS pings
minx, miny, maxx, maxy = gdf_road.total_bounds
points = [Point(random.uniform(minx, maxx), random.uniform(miny, maxy)) for _ in range(n_points)]
gdf_points = gpd.GeoDataFrame({'geometry': points})

# Create a buffer around the road
buffered_polygon = gdf_road.buffer(buffer)

# Color the points that fall within the buffered polygon
points_within = gdf_points[gdf_points.geometry.within(buffered_polygon.unary_union)]
gdf_points = gdf_points.loc[points_within.index]

return gdf_points